/*
 Copyright (c) 2012 Roman Truba

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial
 portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package ru.truba.touchgrallery.GalleryWidget;

import ru.truba.touchgrallery.TouchView.TouchImageView;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;

import java.util.HashMap;
import java.util.List;

/**
 * Class wraps URLs to adapter, then it instantiates <b>UrlTouchImageView</b> objects to paging up through them.
 */
public class BasePagerAdapter extends PageSliderProvider {
    HashMap<Integer,Component> map=new HashMap();
    public BasePagerAdapter(Context context, List<String> resources) {
        this.mResources = resources;
        this.mContext = context;
    }

    protected final List<String> mResources;
    protected final Context mContext;
    protected int mCurrentPosition = -1;
    protected OnItemChangeListener mOnItemChangeListener;


    @Override
    public int getCount() {
        return mResources.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        return null;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return component.equals(o);
    }
    public int getCurrentPosition() { return mCurrentPosition; }


    public void setOnItemChangeListener(OnItemChangeListener listener) { mOnItemChangeListener = listener; }


    public static interface OnItemChangeListener
    {
        public void onItemChange(int currentPosition);
    }
    public    class  pagechangeList implements PageSlider.PageChangedListener
    {
        ComponentContainer componentContainer;
       public pagechangeList(ComponentContainer slider)
        {
            componentContainer=slider;
        }

        @Override
        public void onPageSliding(int i, float v, int i1) {

        }

        @Override
        public void onPageSlideStateChanged(int i) {

        }

        @Override
        public void onPageChosen(int position) {
            if (mCurrentPosition == position) {
                return;
            }
            GalleryViewPager galleryContainer = ((GalleryViewPager)componentContainer);

            if (!map.isEmpty())
            {
                galleryContainer.mCurrentView= (TouchImageView) map.get(position);
                galleryContainer.mCurrentView.resetScale();
            }

            mCurrentPosition = position;
            if (mOnItemChangeListener != null) { mOnItemChangeListener.onItemChange(mCurrentPosition); }
        }
    }
}